var programacions = {
		init:function(){
	$.getJSON("cursos/listacursos", function(listacursos){
		$.getJSON("personas/listadocentes", function(listadocentes) {
			$.getJSON("periodos/listaperiodos", function(listaperiodos) {
				setupGrid(listacursos,listadocentes,listaperiodos);
			});
		});
	});

	var programacion = this;	
		function setupGrid(listacursos,listadocentes,listaperiodos){
			$('#programacions').jqGrid({
	           url:'programacions/index',
	           datatype:'json',
	           mtype:'POST',
	           colNames:['id','Curso','Docente','Año','Seccion','Fecha Inicio','Fecha fin','Creado','Estado'],
	           colModel:[
	                {name:"data[Programacion][id]",hidden:true},
	               		          
	            {name:"data[Programacion][curso_id]",index:'Curso.descripcion',width:80,align:'left',
            	sortable:true,editable:true,editoptions:{size:40, value:listacursos},
         	   formoptions:{ rowpos:1, elmprefix:"(*)"},	  
            	edittype:"select",searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},	
            	
            	{name:"data[Programacion][docente_id]",index:'Persona.nombre_completo',width:80,align:'left',            		
            	sortable:true,editable:true,editoptions:{size:40, value:listadocentes},
         	   formoptions:{ rowpos:2, elmprefix:"(*)"},	  
            	edittype:"select",searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},	
            	
            	{name:"data[Programacion][periodo_id]",index:'Periodo.anio',width:45,align:'left',
            	sortable:true,editable:true,editoptions:{size:40, value:listaperiodos},
         	   formoptions:{ rowpos:3, elmprefix:"(*)"},	  
            	edittype:"select",searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
            	
            	 {name:"data[Programacion][seccion]",index:'Programacion.seccion',width:30,sortable:false,
            		edittype:"select", editoptions:{value:"A:A;B:B"},editable:true,
	            	   formoptions:{ rowpos:4, elmprefix:"(*)"},	  
            		searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
            	
            	{name:"data[Programacion][fecha_inicio]",index:'Programacion.fecha_inicio',width:45,editoptions:{size:15, maxlength:10},
	            	   editoptions:{size:12, 
	            	   dataInit:function(el){ $(el).datepicker({dateFormat:'yy-mm-dd'}); },            	 
	            	   defaultValue: function(){ 
	            		   var currentTime = new Date(); 
	            		   var month = parseInt(currentTime.getMonth() + 1); 
	            		   month = month <= 9 ? "0"+month : month; var day = currentTime.getDate(); 
	            		   day = day <= 9 ? "0"+day : day; var year = currentTime.getFullYear(); 
	            		   return year+"-"+month + "-"+day; } },	            		   
	            	   formoptions:{ rowpos:5, elmprefix:"(*)",elmsuffix:" yyyy-mm-dd" },	            	   
	            	   editable:true,sorttype:"date",editrules:{edithidden:true,date:true,required:false}},
	            	   
        	   {name:"data[Programacion][fecha_fin]",index:'Programacion.fecha_fin',width:45,editoptions:{size:15, maxlength:10},
            	   editoptions:{size:12, 
            	   dataInit:function(el){ $(el).datepicker({dateFormat:'yy-mm-dd'}); },            	 
            	   defaultValue: function(){ 
            		   var currentTime = new Date(); 
            		   var month = parseInt(currentTime.getMonth() + 1); 
            		   month = month <= 9 ? "0"+month : month; var day = currentTime.getDate(); 
            		   day = day <= 9 ? "0"+day : day; var year = currentTime.getFullYear(); 
            		   return year+"-"+month + "-"+day; } },	            		   
            	   formoptions:{ rowpos:6, elmprefix:"(*)",elmsuffix:" yyyy-mm-dd" },	            	   
            	   editable:true,sorttype:"date",editrules:{edithidden:true,date:true,required:false}},
	         	   
            	   {name:"data[Programacion][created]",index:'Programacion.created',width:50,sortable:true,
            		   editable:false,
	            	   formoptions:{ rowpos:7, elmprefix:"(*)"},	  
            		   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
            	   
            	   {name:"data[Programacion][estado]",index:'Estado.descripcion',width:35,sortable:false,editable:true,
            		   edittype:"select", editoptions:{value:"1:Activo;2:Desactivo"},
	            	   formoptions:{ rowpos:8, elmprefix:"(*)"},
            		   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}}	   
            	   
	           ],
		
			   pager:$('#programacions-pager'),
	           rowNum:21,
	           rowList:[10,20,30],
	           mtype: "POST",
	           viewrecords:true,
	           caption:'Programacions',
		        rownumbers: true, 
	           height:'300',
	           sortname: "Programacion.id",
	           sortorder: "desc", 
	           editurl: 'programacions/index'	                    
	       });
			$('#programacions').navGrid("#programacions-pager",{view:true,edit:true,add:true,del:true},
					{clearAfterEdit:false,reloadAfterSubmit:true, bottominfo:"Campos marcados con (*) son requeridos", closeOnEscape:true, afterSubmit:programacion.afterSubmit},
					{clearAfterAdd:true,reloadAfterSubmit:true, bottominfo:"Campos marcados con (*) son requeridos", closeOnEscape:true,afterSubmit:programacion.afterSubmit},
					{width:270,reloadAfterSubmit:true,closeOnEscape:true},
					{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew'],multipleSearch:true,closeOnEscape:true});	       
			$("#programacions").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
	       jQuery("#programacions").jqGrid('setGridWidth',$('#RightPane').width()-50);
			}}
		,
		afterSubmit: function(response){var respuesta = JSON.parse(response.responseText);return [respuesta.respuesta,respuesta.mensaje];}
};
